package com.xdwanj.dao;

import com.xdwanj.entity.BookInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * 图书管理数据库操作接口
 */
@Slf4j
@Repository
public class BookDao {

    @Autowired
    public BaseDao baseDao;

    /**
     * 获取图书列表
     *
     * @param name    图书名字
     * @param classId 图书分类id
     * @return java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     */
    public List<Map<String, Object>> getList(String name, Integer classId) {

        // 拼接查询语句 创建部分sql对象
        StringBuffer tempSql = new StringBuffer("select * from book_info where 1=1 ");
        // 判断图书名字是否为空 不为空则拼接查询语句
        if (name != null && !"".equals(name)) {
            tempSql.append(" and name like '%").append(name).append("%'");
        }
        // 判断图书类型是否为空 不为空则拼接查询语句
        if (classId != null && classId != 0) {
            tempSql.append(" and class_id = ").append(classId);
        }
        // 将创建的sql对象转换为字符串
        String sql = tempSql.toString();
        log.info("sql语句：{}", sql);
        // 执行查询语句
        List<Map<String, Object>> list = baseDao.executeQuery(sql);
        return list;
    }

    /**
     * 删除图书
     *
     * @param id 图书编号
     * @return int
     */
    public int delete(Integer id) {
        // 编写sql语句
        String sql = "delete from book_info where book_id = ?";
        log.info("sql语句：{}", sql);
        // 执行sql语句
        // 返回受影响的行数
        return baseDao.executeUpdate(sql, id);
    }

    /**
     * 修改图书
     *
     * @param book 图书实体类
     * @return int
     */
    public int updateById(BookInfo book) {
        // 编写sql语句
        // 编写sql语句
        String sql = "update book_info set name=?,author=?,publish = ?," +
            "isbn = ?,introduction = ?,language =?,price=?,pub_date=?," +
            "class_id=? ,number =? " +
            "where book_id=?";
        // 执行sql语句
        // 返回受影响的行数
        return baseDao.executeUpdate(sql, book.getName(), book.getAuthor(), book.getPublish(),
            book.getIsbn(), book.getIntroduction(), book.getLanguage(), book.getPrice(),
            book.getPubDate(), book.getClassId(), book.getNumber(), book.getBookId());
    }

    /**
     * 当图书数量大于0时，减一
     *
     * @param bookId 图书编号
     * @return int
     */
    public int updateByIdSetNumbet(Integer bookId) {

        // 编写sql语句
        String sql = "UPDATE book_info\n" +
            "SET number = CASE \n" +
            "                 WHEN number > 0 THEN number - 1 \n" +
            "                 ELSE number \n" +
            "               END\n" +
            "WHERE book_id = ?";
        log.info("sql语句：{}", sql);
        // 执行sql语句
        return baseDao.executeUpdate(sql, bookId);
    }

    /**
     * 图书数量加一
     *
     * @param bookId 图书编号
     * @return int
     */
    public int updateByIdAddNubet(Integer bookId) {

        // 编写sql语句
        String sql = "UPDATE book_info\n" +
            "SET number = CASE \n" +
            "                 WHEN number IS NOT NULL THEN number + 1 \n" +
            "                 ELSE number \n" +
            "               END\n" +
            "WHERE book_id = ?";
        log.info("sql语句：{}", sql);
        // 执行sql语句
        return baseDao.executeUpdate(sql, bookId);
    }

    /**
     * 添加图书
     *
     * @param book 图书实体类
     * @return int
     */

    public int add(BookInfo book) {
        // 编写sql语句
        String sql = "insert into book_info(name,author,publish,isbn,introduction,language,price,pub_date,class_id,number) " +
            "values(?,?,?,?,?,?,?,?,?,?)";
        // 执行sql语句
        // 返回受影响的行数
        return baseDao.executeUpdate(sql, book.getName(), book.getAuthor(), book.getPublish(),
            book.getIsbn(), book.getIntroduction(), book.getLanguage(), book.getPrice(),
            book.getPubDate(), book.getClassId(), book.getNumber());
    }


}